﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Grid;
using System.Text.RegularExpressions;
using GuiLabs.Undo;

namespace Geometry2D.Tracker
{
    public class trackerGrid:DataGrid
    {
        public PilotWindow followPilot { get; set; }
        public TrackList trackerList { get; set; }

        public int countTracker;

        private IAction currentAction;
        private string actionType;

        public trackerGrid(PilotWindow pilot)
        {
            this.Columns.Clear();
            this.Background = Brushes.White;
            trackerList = new TrackList();
            this.AutoGenerateColumns = true;
            this.ItemsSource = trackerList.List;
            this.IsReadOnly = true;
            this.AutoGenerateColumns=true;
            this.followPilot = pilot;
            this.countTracker = 0;

            this.trackerList.mListChanged += new EventHandler(trackerList_mListChanged);
            this.followPilot.DrawingHost.DrawingControl.Drawing.ActionManager.CollectionChanged += new EventHandler(ActionManager_CollectionChanged);
            this.followPilot.DrawingHost.MouseLeftButtonUp += new MouseButtonEventHandler(DrawingHost_MouseLeftButtonUp);
        }

        void DrawingHost_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if(currentAction!=null)
            {
                string figueActionType = currentAction.GetType().ToString();
                figueActionType = figueActionType.Replace("DynamicGeometry.", string.Empty);

                string figueType = string.Empty;
                string figueName = string.Empty;
                string effect = string.Empty;

              
                if (currentAction.GetType() == typeof(DynamicGeometry.GeometryAction))
                {
                    DynamicGeometry.GeometryAction geometryAction = currentAction as DynamicGeometry.GeometryAction;
                }   
                else if (currentAction.GetType() == typeof(DynamicGeometry.MoveAction))
                {
                    DynamicGeometry.MoveAction moveAction = currentAction as DynamicGeometry.MoveAction;
                    List<DynamicGeometry.IMovable> listPoint = moveAction.Points as List<DynamicGeometry.IMovable>;
                    DynamicGeometry.IMovable startPoint = listPoint[0];
                    DynamicGeometry.IMovable endPoint = listPoint[listPoint.Count - 1];
                    List<DynamicGeometry.IFigure> fList = moveAction.ToRecalculate as List<DynamicGeometry.IFigure>;
                    countTracker++;
                    if (countTracker % 10 == 0)
                        trackerList.List.Clear();
                    if (fList != null)
                    {
                        foreach (var obj in fList)
                        {
                            figueName = obj.Name;
                            figueType = obj.GetType().ToString();
                            figueType = figueType.Replace("DynamicGeometry.", string.Empty);
                            //effect = string.Format("[{0},{1}]->[{2},{3}]",
                            //    Math.Round(startPoint.Coordinates.X, 2), Math.Round(startPoint.Coordinates.Y, 2),
                            //    Math.Round(endPoint.Coordinates.X, 2), Math.Round(endPoint.Coordinates.Y, 2));
                            this.Add(new Tracker.trackRecord { Number = countTracker, ActionType = actionType, FigureActionType = figueActionType, FigureType = figueType, FigureName = figueName, Effect = effect });
                        }
                    }
                    else
                    {

                    }
                   
                }
                else if (currentAction.GetType() == typeof(DynamicGeometry.PasteAction))
                {
                    DynamicGeometry.PasteAction pasteAction = currentAction as DynamicGeometry.PasteAction;
                }
                else if (currentAction.GetType() == typeof(DynamicGeometry.ReplaceFigureAction))
                {
                    DynamicGeometry.ReplaceFigureAction replace = currentAction as DynamicGeometry.ReplaceFigureAction;

                } 
            }
            currentAction = null;
        }

        void ActionManager_CollectionChanged(object sender, EventArgs e)
        {
            currentAction = followPilot.DrawingHost.DrawingControl.Drawing.ActionManager.CurrentAction;
            actionType = followPilot.DrawingHost.DrawingControl.Drawing.ActionManager.actionType;
            string figueActionType = currentAction.GetType().ToString();
            figueActionType = figueActionType.Replace("DynamicGeometry.", string.Empty);

            string figueType = string.Empty;
            string figueName = string.Empty;
            string effect = string.Empty;

            if (currentAction.GetType() == typeof(GuiLabs.Undo.Transaction))
            {
                behaviorForTransaction(currentAction);
            }
            if (currentAction.GetType() == typeof(DynamicGeometry.AddFigureAction))
            {
                DynamicGeometry.AddFigureAction addAction = currentAction as DynamicGeometry.AddFigureAction;
                figueName = addAction.Figure.Name;
                figueType = addAction.Figure.GetType().ToString();
                figueType = figueType.Replace("DynamicGeometry.", string.Empty);
                countTracker++;
                if (countTracker % 10 == 0)
                    trackerList.List.Clear();

                this.Add(new Tracker.trackRecord { Number = countTracker, ActionType = actionType, FigureActionType = figueActionType, FigureType = figueType, FigureName = figueName, Effect = effect });
            }
            else if (currentAction.GetType() == typeof(DynamicGeometry.RemoveFigureAction))
            {
                DynamicGeometry.RemoveFigureAction removeFigureAction = currentAction as DynamicGeometry.RemoveFigureAction;
                figueName = removeFigureAction.Figure.Name;
                figueType = removeFigureAction.Figure.GetType().ToString();
                figueType = figueType.Replace("DynamicGeometry.", string.Empty);

                countTracker++;
                if (countTracker % 10 == 0)
                    trackerList.List.Clear();

                this.Add(new Tracker.trackRecord { Number = countTracker, ActionType = actionType, FigureActionType = figueActionType, FigureType = figueType, FigureName = figueName, Effect = effect });
            }
            else if (currentAction.GetType() == typeof(DynamicGeometry.RemoveFiguresAction))
            {
                DynamicGeometry.RemoveFiguresAction removeFiguresAction = currentAction as DynamicGeometry.RemoveFiguresAction;
                countTracker++;
                if (countTracker % 10 == 0)
                    trackerList.List.Clear();
                foreach (var figure in removeFiguresAction.Figures)
                {
                    string name = figure.Name;
                    string type = figure.GetType().ToString();
                    type = type.Replace("DynamicGeometry.", string.Empty);
                    figueName = name;
                    figueType = type;
                    this.Add(new Tracker.trackRecord { Number = countTracker, ActionType = actionType, FigureActionType = figueActionType, FigureType = figueType, FigureName = figueName, Effect = effect });
                }
            }
            else if (currentAction.GetType() == typeof(DynamicGeometry.SetPropertyAction))
            {
                DynamicGeometry.SetPropertyAction setPropertyAction = currentAction as DynamicGeometry.SetPropertyAction;
                object newValue = setPropertyAction.NewValue;
                string typeProperty = setPropertyAction.Property.Name;
                var o = setPropertyAction.Property.Parent;
                DynamicGeometry.IFigure figure = o as DynamicGeometry.IFigure;
                figueName = figure.Name;
                figueType = figure.GetType().ToString();
                figueType = figueType.Replace("DynamicGeometry.", string.Empty);
                effect = string.Format("{0}:{1}", typeProperty, newValue);
                countTracker++;
                if (countTracker % 10 == 0)
                    trackerList.List.Clear();

                this.Add(new Tracker.trackRecord { Number = countTracker, ActionType = actionType, FigureActionType = figueActionType, FigureType = figueType, FigureName = figueName, Effect = effect });
            }
        }

        public void Add(trackRecord record)
        {
            this.trackerList.Add(record);
            this.trackerChannged(this, new EventArgs());
        }

        public void Track(IAction action)
        {
            
        }

        public event EventHandler trackerChannged;

        void trackerList_mListChanged(object sender, EventArgs e)
        {
            this.Items.Refresh();
        }

        public void behaviorForTransaction(IAction currentAction)
        {
            countTracker++;
            if (countTracker % 10 == 0)
                trackerList.List.Clear();
            GuiLabs.Undo.Transaction transaction = currentAction as GuiLabs.Undo.Transaction;
            string figueActionType = "Transaction";
            string figueType = string.Empty;
            string figueName = string.Empty;
            string effect = string.Empty;
            foreach (var action in transaction.Actions)
            {
                if (action.GetType() == typeof(DynamicGeometry.AddFigureAction))
                {
                    DynamicGeometry.AddFigureAction addAction = action as DynamicGeometry.AddFigureAction;
                    figueName = addAction.Figure.Name;
                    figueType = addAction.Figure.GetType().ToString();
                    figueType = figueType.Replace("DynamicGeometry.", string.Empty);                   

                    this.Add(new Tracker.trackRecord { Number = countTracker, ActionType = actionType, FigureActionType = figueActionType, FigureType = figueType, FigureName = figueName, Effect = effect });
                }            
            }
        }


    }
}
